#include "networkmanager.h"
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QSvgRenderer>
#include <QSvgWidget>
#include <QPainter>
#include <QImage>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QFile>
NetworkManager* NetworkManager::manager = nullptr;

void NetworkManager::getSVG(QImage** image)
{
    QNetworkRequest quest(QUrl("https://marketing.cyberspaceit.cn/interface/metromap/maps/linesh.svg"));
    QNetworkReply* reply = _manager->get(quest);
    connect(reply, &QNetworkReply::finished, this, [=]{
        if(reply->error() != QNetworkReply::NoError){
            qDebug() << "svg请求出错" << reply->errorString();
            reply->deleteLater();
            return;
        }
        QSvgRenderer svg;
        svg.load(reply->readAll()); // 加载svg图

        QSize size = svg.defaultSize(); // 获得默认大小
        qDebug() << "svg size = " << size;

        *image = new QImage(size, QImage::Format_ARGB32); // 创建
        (*image)->fill(Qt::transparent);

        QPainter p(*image);
        svg.render(&p);

        reply->deleteLater();
    });
}

void NetworkManager::getStation()
{
    QNetworkRequest quest(QUrl("https://m.shmetro.com/interface/metromap/metromap.aspx?func=stationList"));
    QNetworkReply* reply = _manager->get(quest);
    connect(reply, &QNetworkReply::finished, this, [=]{
        if(reply->error() != QNetworkReply::NoError){
            qDebug() << "svg请求出错" << reply->errorString();
            reply->deleteLater();
            return;
        }
        QByteArray Data = reply->readAll();
        QJsonDocument Doc = QJsonDocument::fromJson(Data);
        QJsonObject Root = Doc.object();
        QJsonArray array = Root.value("levels").toArray().at(0).toObject().value("locations").toArray();
        QFile file("station.csv");
        file.open(QIODevice::WriteOnly | QIODevice::NewOnly);
        QTextStream out(&file);
        for(int i = 0; i < array.size(); ++i){
            QJsonObject cur = array[i].toObject();
            QString id = cur.value("id").toString();
            QString title = cur.value("title").toString();
            QString line = id + "," + title + ",,\n";
            out << line;
        }
    });
}

void NetworkManager::getPos(QHash<QString, QSharedPointer<Node> >* _id_node, bool* ready)
{
    QNetworkRequest quest(QUrl("https://m.shmetro.com/interface/metromap/metromap.aspx?func=stationList"));
    QNetworkReply* reply = _manager->get(quest);
    connect(reply, &QNetworkReply::finished, this, [=]{
        if(reply->error() != QNetworkReply::NoError){
            qDebug() << "svg请求出错" << reply->errorString();
            reply->deleteLater();
            return;
        }
        QByteArray Data = reply->readAll();
        QJsonDocument Doc = QJsonDocument::fromJson(Data);
        QJsonObject Root = Doc.object();
        QJsonArray array = Root.value("levels").toArray().at(0).toObject().value("locations").toArray();

        for(int i = 0; i < array.size(); ++i){
            QJsonObject cur = array[i].toObject();
            QString id = cur.value("id").toString();
            if(_id_node->count(id)){
                float x = cur.value("x").toString().toFloat();
                float y = cur.value("y").toString().toFloat();
                (*_id_node)[id]->pos = {x*3000 + 3000, y*3000};
                qDebug() << id << "的位置" << (*_id_node)[id]->pos;
            } else {
                qDebug() << id << "未找到";
            }
        }
        *ready = true;
    });
}

//void NetworkManager::getStation()
//{
//    QNetworkRequest quest(QUrl("https://m.shmetro.com/core/shmetro/mdstationinfoback_new.ashx?act=getAllStations"));
//    QNetworkReply* reply = _manager->get(quest);
//    connect(reply, &QNetworkReply::finished, this, [=]{
//        if(reply->error() != QNetworkReply::NoError){
//            qDebug() << "svg请求出错" << reply->errorString();
//            reply->deleteLater();
//            return;
//        }
//        QByteArray Data = reply->readAll();
//        QJsonDocument Doc = QJsonDocument::fromJson(Data);
//        QJsonArray array = Doc.array();
//        QFile file("station.csv");
//        file.open(QIODevice::WriteOnly | QIODevice::NewOnly);
//        QTextStream out(&file);
//        for(int i = 0; i < array.size(); ++i){
//            QJsonObject cur = array[i].toObject();
//            QString id = cur.value("key").toString();
//            QString title = cur.value("value").toString();
//            QString line = id + "," + title + ",,\n";
//            out << line;
//        }
//    });
//}

NetworkManager::NetworkManager(QObject *parent)
    : QObject(parent), _manager(new QNetworkAccessManager(this))
{}

// https://m.shmetro.com/interface/metromap/metromap.aspx?func=lines
//[{"line_no":1,"color":"#e3002b","bgcolor":"#fdeae9"},{"line_no":2,"color":"#8cc220","bgcolor":"#EBF7EC"},{"line_no":3,"color":"#fcd600","bgcolor":"#fffee5"},{"line_no":4,"color":"#461d84","bgcolor":"#f1ebf4"},{"line_no":5,"color":"#944d9a","bgcolor":"#e8d2f0"},{"line_no":6,"color":"#d40068","bgcolor":"#ffcae4"},{"line_no":7,"color":"#ed6f00","bgcolor":"#ffcc99"},{"line_no":8,"color":"#0094d8","bgcolor":"#60b7d4"},{"line_no":9,"color":"#87caed","bgcolor":"#85C6DA"},{"line_no":10,"color":"#c6afd4","bgcolor":"#e0c5f0"},{"line_no":11,"color":"#871c2b","bgcolor":"#BB8866"},{"line_no":12,"color":"#007a60","bgcolor":"#99CBC1"},{"line_no":13,"color":"#e999c0","bgcolor":"#f4b8d2"},{"line_no":14,"color":"#616020","bgcolor":"#9a982f"},{"line_no":15,"color":"#c8b38e","bgcolor":"#f9e7c8"},{"line_no":16,"color":"#98d1c0","bgcolor":"#C6E8DF"},{"line_no":17,"color":"#bb796f","bgcolor":"#ebd6d3"},{"line_no":18,"color":"#C09453","bgcolor":"#C09453"},{"line_no":41,"color":"#b5b6b6","bgcolor":"#f2f7f7"},{"line_no":51,"color":"#cccccc","bgcolor":"#dddddd"}]
